﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace RealEstateCenter
{
    public partial class EditEstate : System.Web.UI.Page
    {
        System.Data.SqlClient.SqlConnection sqlconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            new login_check("estate_center");
            if (!Page.IsPostBack)
            {
                estateID.Text = Request.QueryString["id"];
                Session["edit_estate_id"] = Request.QueryString["id"];
                SqlCommand cmd = new SqlCommand("select * from Estate where estate_id='" + Request.QueryString["id"] + "'", sqlconnect);
                sqlconnect.Open();
                SqlDataReader dtrReader = cmd.ExecuteReader();
                if (dtrReader.Read())
                {
                    estate_address.Text = dtrReader["estate_address"].ToString();
                    estate_city.SelectedValue = dtrReader["estate_city"].ToString();
                    estate_room_number.Text = dtrReader["estate_room_number"].ToString();
                    estate_description.Text = dtrReader["estate_description"].ToString();
                    estate_price.Text = dtrReader["estate_price"].ToString();
                    estate_heating_type.Text = dtrReader["estate_heating_type"].ToString();
                    estate_type.Text = dtrReader["estate_type"].ToString();
                    estate_floor.Text = dtrReader["estate_floor"].ToString();
                    estate_agency_id.Text = dtrReader["estate_agency_id"].ToString();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string edit_estate_id = Session["edit_estate_id"].ToString();
            SqlCommand cmd = new SqlCommand("update Estate set estate_address='" + estate_address.Text + "', estate_city='" + estate_city.Text + "', estate_room_number='" + estate_room_number.Text + "',estate_description='" + estate_description.Text + "', estate_price='" + estate_price.Text + "',estate_heating_type='" + estate_heating_type.Text + "',estate_type='" + estate_type.Text + "',estate_floor='" + estate_floor.Text + "',estate_agency_id='" + estate_agency_id.Text + "' where estate_id=" + Session["edit_estate_id"].ToString(), sqlconnect);
            sqlconnect.Open();
            int kontrol = cmd.ExecuteNonQuery();

            if (kontrol == 1)
            {
                Response.Redirect("ListEstates.aspx");
            }
        }
    }
}